﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;

#endregion

namespace ScrumTable.Common.Threading
{
    /// <summary>
    /// Specifies the result data of an asynchronous call.
    /// </summary>
    /// <typeparam name="T">Specifies the result data type.</typeparam>
    public class DataResult<T> : IDataResult
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the result data of the async call.
        /// </summary>
        public T Data
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets a user-defined object that qualifies or contains information about an asynchronous operation.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// A user-defined object that qualifies or contains information about an asynchronous operation.
        /// </returns>
        public object AsyncState
        {
            get { return Data; }
        }

        /// <summary>
        /// Gets a <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// A <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.
        /// </returns>
        public virtual System.Threading.WaitHandle AsyncWaitHandle
        {
            get { return null; }
        }

        /// <summary>
        /// Gets a value that indicates whether the asynchronous operation completed synchronously.
        /// </summary>
        /// <value></value>
        /// <returns>true if the asynchronous operation completed synchronously; otherwise, false.
        /// </returns>
        public virtual bool CompletedSynchronously
        {
            get { return false; }
        }

        /// <summary>
        /// Gets a value that indicates whether the asynchronous operation has completed.
        /// </summary>
        /// <value></value>
        /// <returns>true if the operation is complete; otherwise, false.
        /// </returns>
        public virtual bool IsCompleted
        {
            get { return (Error == null); }
        }

        /// <summary>
        /// Gets a value that indicates whether the has completed with an error.
        /// </summary>
        public virtual Exception Error
        {
            get;
            private set;
        }

        /// <summary>
        /// Returns true if the asynchronous process has returned an error.
        /// </summary>
        public bool HasError
        {
            get { return (Error != null); }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataResult instance with the specified data.
        /// </summary>
        /// <param name="data">Result data to pass to the async callback receiver.</param>
        public DataResult(T data)
        {
            Data = data;
        }

        /// <summary>
        /// Creates a new DataResult instance with the specified data.
        /// </summary>
        /// <param name="data">Result data to pass to the async callback receiver.</param>
        /// <param name="error">occurred error which is passed to the callback receiver.</param>
        public DataResult(T data, Exception error)
        {
            Data = data;
            Error = error;
        }


        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }


    /// <summary>
    /// Gets the result data of an asynchronous call.
    /// </summary>
    public class DataResult : DataResult<object>
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets a readonly empty data result instance.
        /// </summary>
        public static readonly DataResult Empty = new DataResult();

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataResult instance with no data.
        /// </summary>
        public DataResult()
            : base(null)
        {

        }

        /// <summary>
        /// Creates a new DataResult instance with the specified data.
        /// </summary>
        /// <param name="error">occurred error which is passed to the callback receiver.</param>
        public DataResult(Exception error)
            : base(null, error)
        {

        }

        #endregion
    }
}